﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

using FluentAssertions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Ncqrs.Denormalization;

using TheKitchen.Events;

namespace TheKitchen.Host.Specs
{
    [TestClass]
    public class DenormalizerRegistrySpecs
    {
        [TestMethod]
        public void When_denormalizing_an_event_it_should_call_the_registered_denormalizer()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var denormalizer = new TestDenormalizer();
            
            var registry = new DenormalizerRegistry();
            registry.RegisterDenormalizer(typeof(NewRecipeCreated), denormalizer);

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var evt = new NewRecipeCreated { Id = Guid.NewGuid() };

            registry.PublishEvent(evt);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            denormalizer.LastEvent.Should().BeSameAs(evt, "the event should have passed to our denormalizer");
        }

        private class TestDenormalizer : IDenormalizes<NewRecipeCreated>
        {
            public void DenormalizeEvent(NewRecipeCreated evnt)
            {
                LastEvent = evnt;
            }

            public NewRecipeCreated LastEvent { get; set; }
        }
    }
}
